<template>
	<view class="forgot-container">
		<custom-navbar title="忘记密码" :showBack="true"></custom-navbar>
		
		<view class="main-content" :style="{paddingTop: navbarTotalHeight + 'px'}">
			<!-- 步骤指示器 -->
			<view class="step-indicator">
				<view class="step-item" :class="{active: currentStep >= 1, completed: currentStep > 1}">
					<view class="step-number">1</view>
					<text class="step-text">验证手机号</text>
				</view>
				<view class="step-line" :class="{active: currentStep > 1}"></view>
				<view class="step-item" :class="{active: currentStep >= 2, completed: currentStep > 2}">
					<view class="step-number">2</view>
					<text class="step-text">重置密码</text>
				</view>
				<view class="step-line" :class="{active: currentStep > 2}"></view>
				<view class="step-item" :class="{active: currentStep >= 3}">
					<view class="step-number">3</view>
					<text class="step-text">完成</text>
				</view>
			</view>
			
			<!-- 步骤1：验证手机号 -->
			<view class="form-container" v-if="currentStep === 1">
				<view class="form-header">
					<text class="form-title">验证手机号</text>
					<text class="form-desc">请输入您注册时使用的手机号</text>
				</view>
				
				<view class="form-content">
					<view class="form-group">
						<image class="input-icon" src="https://img.icons8.com/ios/50/phone.png" mode="aspectFit"></image>
						<input 
							class="input-field" 
							placeholder="请输入手机号" 
							type="number" 
							v-model="phone"
							maxlength="11"
						/>
					</view>
					
					<view class="form-group">
						<image class="input-icon" src="https://img.icons8.com/ios/50/sms.png" mode="aspectFit"></image>
						<input 
							class="input-field" 
							placeholder="请输入验证码" 
							v-model="verifyCode"
							maxlength="6"
						/>
						<view class="code-btn" @tap="sendCode" :class="{disabled: codeCountdown > 0}">
							<text class="code-text">
								{{codeCountdown > 0 ? `${codeCountdown}s` : '获取验证码'}}
							</text>
						</view>
					</view>
					
					<view class="submit-btn" @tap="verifyPhone">
						<text class="btn-text">下一步</text>
					</view>
				</view>
			</view>
			
			<!-- 步骤2：重置密码 -->
			<view class="form-container" v-if="currentStep === 2">
				<view class="form-header">
					<text class="form-title">重置密码</text>
					<text class="form-desc">请设置新的登录密码</text>
				</view>
				
				<view class="form-content">
					<view class="form-group">
						<image class="input-icon" src="https://img.icons8.com/ios/50/lock.png" mode="aspectFit"></image>
						<input 
							class="input-field" 
							placeholder="请输入新密码" 
							:password="!showPassword"
							v-model="newPassword"
						/>
						<image 
							class="eye-icon" 
							:src="showPassword ? 'https://img.icons8.com/ios/50/visible.png' : 'https://img.icons8.com/ios/50/invisible.png'" 
							mode="aspectFit"
							@tap="togglePassword"
						></image>
					</view>
					
					<view class="form-group">
						<image class="input-icon" src="https://img.icons8.com/ios/50/lock.png" mode="aspectFit"></image>
						<input 
							class="input-field" 
							placeholder="请确认新密码" 
							:password="!showConfirmPassword"
							v-model="confirmPassword"
						/>
						<image 
							class="eye-icon" 
							:src="showConfirmPassword ? 'https://img.icons8.com/ios/50/visible.png' : 'https://img.icons8.com/ios/50/invisible.png'" 
							mode="aspectFit"
							@tap="toggleConfirmPassword"
						></image>
					</view>
					
					<view class="password-tips">
						<text class="tip-text">密码要求：</text>
						<text class="tip-item">• 长度不少于6位</text>
						<text class="tip-item">• 建议包含字母和数字</text>
					</view>
					
					<view class="submit-btn" @tap="resetPassword">
						<text class="btn-text">确认重置</text>
					</view>
				</view>
			</view>
			
			<!-- 步骤3：完成 -->
			<view class="success-container" v-if="currentStep === 3">
				<image class="success-icon" src="https://img.icons8.com/fluency/96/checked.png" mode="aspectFit"></image>
				<text class="success-title">密码重置成功</text>
				<text class="success-desc">您的密码已成功重置，请使用新密码登录</text>
				
				<view class="success-actions">
					<view class="action-btn primary" @tap="goToLogin">
						<text class="action-text">立即登录</text>
					</view>
					<view class="action-btn secondary" @tap="goToHome">
						<text class="action-text">返回首页</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import CustomNavbar from '@/components/custom-navbar/custom-navbar.vue';

export default {
	components: {
		CustomNavbar
	},
	data() {
		return {
			navbarTotalHeight: 132,
			statusBarHeight: 44,
			currentStep: 1,
			phone: '',
			verifyCode: '',
			newPassword: '',
			confirmPassword: '',
			showPassword: false,
			showConfirmPassword: false,
			codeCountdown: 0,
			codeTimer: null
		}
	},
	mounted() {
		const systemInfo = uni.getSystemInfoSync();
		this.statusBarHeight = systemInfo.statusBarHeight || 44;
		this.navbarTotalHeight = this.statusBarHeight + 44;
	},
	onUnload() {
		if (this.codeTimer) {
			clearInterval(this.codeTimer);
		}
	},
	methods: {
		// 发送验证码
		sendCode() {
			if (this.codeCountdown > 0) return;
			
			if (!this.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return;
			}
			
			if (!/^1[3-9]\d{9}$/.test(this.phone)) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				});
				return;
			}
			
			uni.showToast({
				title: '验证码已发送',
				icon: 'success'
			});
			
			this.codeCountdown = 60;
			this.codeTimer = setInterval(() => {
				this.codeCountdown--;
				if (this.codeCountdown <= 0) {
					clearInterval(this.codeTimer);
				}
			}, 1000);
		},
		
		// 验证手机号
		verifyPhone() {
			if (!this.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return;
			}
			
			if (!/^1[3-9]\d{9}$/.test(this.phone)) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				});
				return;
			}
			
			if (!this.verifyCode) {
				uni.showToast({
					title: '请输入验证码',
					icon: 'none'
				});
				return;
			}
			
			// 模拟验证
			uni.showLoading({
				title: '验证中...'
			});
			
			setTimeout(() => {
				uni.hideLoading();
				this.currentStep = 2;
			}, 1500);
		},
		
		// 切换密码可见性
		togglePassword() {
			this.showPassword = !this.showPassword;
		},
		
		// 切换确认密码可见性
		toggleConfirmPassword() {
			this.showConfirmPassword = !this.showConfirmPassword;
		},
		
		// 重置密码
		resetPassword() {
			if (!this.newPassword) {
				uni.showToast({
					title: '请输入新密码',
					icon: 'none'
				});
				return;
			}
			
			if (this.newPassword.length < 6) {
				uni.showToast({
					title: '密码长度不能少于6位',
					icon: 'none'
				});
				return;
			}
			
			if (!this.confirmPassword) {
				uni.showToast({
					title: '请确认新密码',
					icon: 'none'
				});
				return;
			}
			
			if (this.newPassword !== this.confirmPassword) {
				uni.showToast({
					title: '两次输入的密码不一致',
					icon: 'none'
				});
				return;
			}
			
			// 模拟重置密码
			uni.showLoading({
				title: '重置中...'
			});
			
			setTimeout(() => {
				uni.hideLoading();
				this.currentStep = 3;
			}, 2000);
		},
		
		// 前往登录页面
		goToLogin() {
			uni.navigateTo({
				url: '/pages/login/login'
			});
		},
		
		// 返回首页
		goToHome() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		}
	}
}
</script>

<style scoped>
.forgot-container {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	min-height: 100vh;
}

.main-content {
	padding: 40rpx;
}

/* 步骤指示器 */
.step-indicator {
	display: flex;
	align-items: center;
	justify-content: center;
	margin-bottom: 60rpx;
}

.step-item {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.step-number {
	width: 60rpx;
	height: 60rpx;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.3);
	color: rgba(255, 255, 255, 0.8);
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 24rpx;
	font-weight: bold;
	margin-bottom: 10rpx;
	transition: all 0.3s ease;
}

.step-item.active .step-number {
	background: #ffffff;
	color: #667eea;
}

.step-item.completed .step-number {
	background: #4CAF50;
	color: #ffffff;
}

.step-text {
	font-size: 22rpx;
	color: rgba(255, 255, 255, 0.8);
	transition: all 0.3s ease;
}

.step-item.active .step-text {
	color: #ffffff;
	font-weight: bold;
}

.step-line {
	width: 80rpx;
	height: 2rpx;
	background: rgba(255, 255, 255, 0.3);
	margin: 0 20rpx;
	transition: all 0.3s ease;
}

.step-line.active {
	background: #ffffff;
}

/* 表单容器 */
.form-container {
	background: #ffffff;
	border-radius: 20rpx;
	padding: 40rpx;
	box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.1);
}

.form-header {
	text-align: center;
	margin-bottom: 40rpx;
}

.form-title {
	display: block;
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 10rpx;
}

.form-desc {
	display: block;
	font-size: 26rpx;
	color: #666;
}

.form-content {
	/* 表单内容样式 */
}

.form-group {
	position: relative;
	margin-bottom: 30rpx;
	display: flex;
	align-items: center;
	background: #f8f9fa;
	border-radius: 50rpx;
	padding: 20rpx 30rpx;
}

.input-icon {
	width: 40rpx;
	height: 40rpx;
	margin-right: 20rpx;
	opacity: 0.6;
}

.input-field {
	flex: 1;
	font-size: 30rpx;
	color: #333;
	background: transparent;
	border: none;
}

.eye-icon {
	width: 36rpx;
	height: 36rpx;
	opacity: 0.6;
}

.code-btn {
	background: #667eea;
	border-radius: 30rpx;
	padding: 12rpx 24rpx;
	transition: all 0.3s ease;
}

.code-btn.disabled {
	background: #ccc;
}

.code-text {
	font-size: 24rpx;
	color: #ffffff;
}

.password-tips {
	background: #f0f7ff;
	border-radius: 12rpx;
	padding: 20rpx;
	margin-bottom: 30rpx;
}

.tip-text {
	display: block;
	font-size: 26rpx;
	color: #333;
	font-weight: bold;
	margin-bottom: 10rpx;
}

.tip-item {
	display: block;
	font-size: 24rpx;
	color: #666;
	margin-bottom: 5rpx;
}

.submit-btn {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 50rpx;
	padding: 30rpx;
	text-align: center;
	margin-top: 20rpx;
	box-shadow: 0 8rpx 20rpx rgba(102, 126, 234, 0.3);
}

.btn-text {
	font-size: 32rpx;
	color: #ffffff;
	font-weight: bold;
}

/* 成功页面 */
.success-container {
	text-align: center;
	background: #ffffff;
	border-radius: 20rpx;
	padding: 60rpx 40rpx;
	box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.1);
}

.success-icon {
	width: 120rpx;
	height: 120rpx;
	margin-bottom: 30rpx;
}

.success-title {
	display: block;
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 15rpx;
}

.success-desc {
	display: block;
	font-size: 28rpx;
	color: #666;
	margin-bottom: 50rpx;
	line-height: 1.5;
}

.success-actions {
	display: flex;
	gap: 20rpx;
}

.action-btn {
	flex: 1;
	padding: 30rpx;
	border-radius: 50rpx;
	text-align: center;
}

.action-btn.primary {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	box-shadow: 0 8rpx 20rpx rgba(102, 126, 234, 0.3);
}

.action-btn.secondary {
	background: #f8f9fa;
	border: 2rpx solid #e9ecef;
}

.action-btn.primary .action-text {
	color: #ffffff;
	font-weight: bold;
}

.action-btn.secondary .action-text {
	color: #666;
}

.action-text {
	font-size: 30rpx;
}
</style>